﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using TCG;

/// <summary>
/// 编辑器中用到的Context Menu
/// </summary>
public static class TCGContextMenu
{
    /// <summary>
    /// 序列化选中的材质
    /// </summary>

    [MenuItem ("Assets/TCG Game/Serialize Material(s)", false, 0)]
    public static void SerializeMaterial ()
    {
        if (Selection.objects == null || Selection.objects.Length == 0) return;

        foreach (Object currentObj in Selection.objects)
        {
            if (!(currentObj is Material))
            {
                continue;
            }

            MaterialSerializer.Serialize (currentObj as Material);
        }
    }

	[MenuItem ("Assets/TCG Game/Deserialize Material", false, 1)]
	public static void DeSerializeMaterial ()
	{
		string sourcePath = AssetDatabase.GetAssetPath (Selection.activeObject);
		string filePath = Path.Combine (Application.dataPath, "../" + sourcePath);
		Stream fileStream = new FileStream (filePath, FileMode.Open);
		MaterialData data = ProtoBuf.Serializer.Deserialize<MaterialData> (fileStream);
		Debug.Log (data.ToString ());
		fileStream.Close ();
	}

	[MenuItem ("Assets/TCG Game/Serialize Texture(s)", false, 2)]
	public static void SerializeTexture ()
	{
		if (Selection.objects == null || Selection.objects.Length == 0) return;

		foreach (Object _currentObj in Selection.objects)
		{
			if (! (_currentObj is Texture))
			{
				continue;
			}

			TextureSerializer.Serialize (_currentObj as Texture);
		}
	}

	[MenuItem ("Assets/TCG Game/Serialize Prefab(s)", false, 3)]
	public static void SerializePrefab ()
	{
		if (Selection.objects == null || Selection.objects.Length == 0) return;

		foreach (Object _currentObj in Selection.objects)
		{
			if (! (_currentObj is GameObject))
			{
				continue;
			}

			PrefabSerializer.Serialize (_currentObj as GameObject);
		}
	}

	[MenuItem ("Assets/TCG Game/Deserialize Prefab", false, 4)]
	public static void DeserializePrefab ()
	{

	}
}